/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLPropertySheet.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPGLPropertySheet

IMPLEMENT_DYNAMIC(CPGLPropertySheet, CPropertySheet)

CPGLPropertySheet::CPGLPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
}

CPGLPropertySheet::CPGLPropertySheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
}

CPGLPropertySheet::~CPGLPropertySheet()
{
}


BEGIN_MESSAGE_MAP(CPGLPropertySheet, CPropertySheet)
	//{{AFX_MSG_MAP(CPGLPropertySheet)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPGLPropertySheet message handlers

BOOL CPGLPropertySheet::OnInitDialog() 
{
	BOOL bResult = CPropertySheet::OnInitDialog();
	
	PGL_ASSERT_VALID(m_pRegion);

	// creating tree
	RECT rc;
	GetWindowRect (&rc);
	// Increase the height of the CPropertySheet by 30
	rc.right += 200;
	// Resize the CPropertySheet
	MoveWindow (rc.left+200, rc.top, rc.right, rc.bottom-rc.top);
	// Convert to client coordinates
	ScreenToClient (&rc);

	
	// filling tree
	FillTree();

	return bResult;
}

void CPGLPropertySheet::FillTree()
{
	PGL_ASSERT_VALID(m_pRegion);
	CString str;
	//////////////////////////////////
	// filling tree...
	// getting axis
	CPGLAxe2DStPtr pAxe=m_pRegion->GetAxe();
	PGL_ASSERT_VALID(pAxe);
	// adding to tree
	HTREEITEM pAxeItem;
	pAxeItem=m_tree.InsertItem(TVIF_TEXT | TVIF_PARAM | TVIF_STATE  /* nMask*/, 
				_T("Axis") /* lpszItem*/, 
				0 /* nImage */, 
				0 /* nSelectedImage */, 
				TVIS_BOLD  /* nState */, 
				TVIS_OVERLAYMASK | TVIS_STATEIMAGEMASK /* nStateMask */, 
				pAxe->GetID() /*  lParam */, 
				TVI_ROOT /* hParent */, 
				TVI_LAST /* hInsertAfter */);
	ASSERT(pAxeItem!=NULL);

	// Adding objects
	// getting objects
	CPGLObjectManager& pObjMg=m_pRegion->GetObjectManager();
	CPGLObjectPtr pObj;

	// inserting object root
	HTREEITEM pObjMgItem;
	str.Format(_T("Objects (%i)"),pObjMg.GetCount());
	pObjMgItem=m_tree.InsertItem(TVIF_TEXT | TVIF_PARAM | TVIF_STATE  /* nMask*/, 
				str /* lpszItem*/, 
				0 /* nImage */, 
				0 /* nSelectedImage */, 
				TVIS_BOLD  /* nState */, 
				TVIS_OVERLAYMASK | TVIS_STATEIMAGEMASK /* nStateMask */, 
				-1 /*  lParam */, 
				TVI_ROOT /* hParent */, 
				TVI_LAST /* hInsertAfter */);
	ASSERT(pObjMgItem!=NULL);

	// filling up insert structure
	CPGLObjectManager::Container::iterator it;

	for (it = pObjMg.begin() ; it != pObjMg.end(); ++it)
	{
		pObj=*it;
		if (!pObj)
			continue;

		// inserting info
		m_tree.InsertItem(TVIF_TEXT | TVIF_PARAM /* nMask*/, 
				pObj->GetName() /* lpszItem*/, 
				0 /* nImage */, 
				0 /* nSelectedImage */, 
				0 /* nState */, 
				0 /* nStateMask */, 
				(long)pObj->GetID() /*  lParam */, 
				pObjMgItem /* hParent */, 
				TVI_LAST /* hInsertAfter */);
	}
}
